package com.mtiiot.fstask.task;

import com.mtiiot.fscore.bean.MetadataInfo;
import com.mtiiot.fscore.inter1.LogService1;
import com.mtiiot.fscore.inter1.RedisService1;
import com.mtiiot.fscore.utils.LogLevel;
import com.mtiiot.fstask.esdao.BaseElasticsearch;
import com.mtiiot.fstask.utils.Contants;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import com.enterprisedt.net.ftp.FileTransferOutputStream;

import java.util.List;


/**
 * Created by Administrator on 16-6-22.
 */
//@Component("logAlerted")
public class FtpTask {
    private static final Logger logger = LogManager.getLogger(FtpTask.class);
    private ThreadPoolTaskExecutor taskExecutor;

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    private int timeout;
    private RedisService1 redisService1;
    private LogService1 logService1;



    private BaseElasticsearch baseElasticsearch;

    public static Logger getLogger() {
        return logger;
    }

    public ThreadPoolTaskExecutor getTaskExecutor() {
        return taskExecutor;
    }
    public void setTaskExecutor(ThreadPoolTaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }


    public RedisService1 getRedisService1() {
        return redisService1;
    }

    public void setRedisService1(RedisService1 redisService1) {
        this.redisService1 = redisService1;
    }

    public LogService1 getLogService1() {
        return logService1;
    }

    public void setLogService1(LogService1 logService1) {
        this.logService1 = logService1;
    }
    public BaseElasticsearch getBaseElasticsearch() {
        return baseElasticsearch;
    }

    public void setBaseElasticsearch(BaseElasticsearch baseElasticsearch) {
        this.baseElasticsearch = baseElasticsearch;
    }


    public synchronized void  execute() {
        //logger.info("========ftp定时任务开始执行==========");

        try {

            Long len = redisService1.llen(new MetadataInfo(Contants.SYS_ID_FS),Contants.FTP_REDISKEY_001);
            if (len!=null&&len > 0) {
                logger.info("本次ftp定时任务:"+len+"个");
                List<String> list = redisService1.lrange(new MetadataInfo(Contants.SYS_ID_FS),Contants.FTP_REDISKEY_001, 0, len);
                for (String str : list) {
                    Long result=redisService1.lrem(new MetadataInfo(Contants.SYS_ID_FS),Contants.FTP_REDISKEY_001,-1,str);
                    if(result!=1){
                        //删除失败
                        logger.warn("================redis 删除数据失败!!=========");
                        continue;
                    }
//                    System.out.println("activeCount:"+taskExecutor.getActiveCount());
//                    System.out.println("maxPoolSize:"+taskExecutor.getMaxPoolSize());
                    //taskExecutor.execute(new FtpThreadTask(str,logService1,redisService1,timeout));
                    taskExecutor.execute(new FtpThreadTask(str,logService1,redisService1,timeout,baseElasticsearch));

                }
            }else{
                //logger.info("本次ftp定时任务没有要执行的任务");
            }
        } catch (Exception e) {
            //logService.sendLog(SysName.SYSNAME_FS, LogLevel.ERROR, Contants.MODULE_FSTASK, "FtpTask", "execute", "", e.getMessage(), Contants.FSTASK_HOST);
            logService1.sendLog(new MetadataInfo(Contants.SYS_ID_FS,Contants.FSTASK_HOST,Contants.MODULE_FSTASK, "FtpTask", "execute"),LogLevel.ERROR,"",e.getMessage());
            logger.error(e.getMessage(), e);
        }
    }
}
